import pika

def callback(ch, method, properties, body):
    print(f"Received message: {body}")
    # 模拟任务处理
    ch.basic_ack(delivery_tag=method.delivery_tag)

connection_params = pika.ConnectionParameters('localhost')
connection = pika.BlockingConnection(connection_params)
channel = connection.channel()

# 声明队列
channel.queue_declare(queue='work_queue', durable=True)

# 设置每次推送一条消息
channel.basic_qos(prefetch_count=1)

# 设置消费者回调函数
channel.basic_consume(queue='work_queue', on_message_callback=callback)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
